package com.ktdaddy.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * @author kdaddy@163.com
 * @date 2021/4/28 22:31
 */
public class ShiroUtils {
    /**
     * PWD_ALGORITHM_NAME: 密码加密算法
     */
    public static final String PWD_ALGORITHM_NAME = "md5";

    /**
     * PWD_ALGORITHM_NAME: 密码加密次数
     */
    public static final int PWD_HASH_ITERATIONS = 2;

    /**
     * 获取盐
     */
    public static String getSalt(){
        RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        String salt = randomNumberGenerator.nextBytes().toHex();
        return salt;
    }


    /**
     * 获取通过用户名加密之后的密码
     * @param salt 盐
     * @param password 密码
     * @param kernelName 用户名
     * @return
     */
    public static String getEncryptPassword(String salt,String password,String kernelName){
        return new SimpleHash(PWD_ALGORITHM_NAME,password,
                ByteSource.Util.bytes(kernelName + salt),PWD_HASH_ITERATIONS).toHex();
    }
}
